<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Module as ModuleModel;

use app\admin\validate\Module as ModuleValidate;
use think\exception\ValidateException;

use think\facade\View;
use think\facade\Request;

/**
 * 权限组
 * Class AuthGroup
 */
class Module extends Base
{
    /**
     * 权限组
     * @return mixed
     */
    public function index()
    {
        $module_list = ModuleModel::select();
		
		View::assign('module_list',$module_list);
		return View::fetch('index');
    }

    /**
     * 添加权限组
     * @return mixed
     */
    public function add()
    {
        if (Request::isPost()) {

			$data	= Request::only(['title', 'name', 'description', 'sort', 'status']);

            try {
				validate(ModuleValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (ModuleModel::create($data)) {
				$this->success('保存成功',url('admin/Module/index'));
			} else {
				$this->error('保存失败');
			}

        }

		return View::fetch('add');
    }

    /**
     * 编辑权限组
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {

		if (Request::isPost()) {

			$data	= Request::only(['id', 'title', 'name', 'description', 'sort', 'status']);
			
            try {
				validate(ModuleValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (ModuleModel::update($data, ['id' => $id])) {
				$this->success('保存成功',url('admin/Module/index'));
			} else {
				$this->error('保存失败');
			}

        }


        $module = ModuleModel::find($id);
		
		View::assign('module',$module);
		return View::fetch('edit');
    }


    /**
     * 删除权限组
     * @param $id
     */
    public function delete($id)
    {
		
        if ($id == 1) {
            $this->error('超级管理组不可删除');
        }
		
		$module = ModuleModel::find($id);

        if ($module->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


}